package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.domain.Attachements;
import com.ruoyi.system.service.IAttachementsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 附件 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-07-16
 */
@Controller
@RequestMapping("/system/attachements")
public class AttachementsController extends BaseController
{
    private String prefix = "system/attachements";
	
	@Autowired
	private IAttachementsService attachementsService;
	
	@RequiresPermissions("system:attachements:view")
	@GetMapping()
	public String attachements()
	{
	    return prefix + "/attachements";
	}
	
	/**
	 * 查询附件列表
	 */
	@RequiresPermissions("system:attachement:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Attachements attachements)
	{
		startPage();
        List<Attachements> list = attachementsService.selectAttachementsList(attachements);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出附件列表
	 */
	@RequiresPermissions("system:attachements:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Attachements attachements)
    {
    	List<Attachements> list = attachementsService.selectAttachementsList(attachements);
        ExcelUtil<Attachements> util = new ExcelUtil<Attachements>(Attachements.class);
        return util.exportExcel(list, "attachements");
    }
	
	/**
	 * 新增附件
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存附件
	 */
	@RequiresPermissions("system:attachements:add")
	@Log(title = "附件", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Attachements attachements)
	{		
		return toAjax(attachementsService.insertAttachements(attachements));
	}

	/**
	 * 修改附件
	 */
	@GetMapping("/edit/{attachmentsId}")
	public String edit(@PathVariable("attachmentsId") Integer attachmentsId, ModelMap mmap)
	{
		Attachements attachements = attachementsService.selectAttachementsById(attachmentsId);
		mmap.put("attachement", attachements);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存附件
	 */
	@RequiresPermissions("system:attachements:edit")
	@Log(title = "附件", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Attachements attachements)
	{		
		return toAjax(attachementsService.updateAttachements(attachements));
	}
	
	/**
	 * 删除附件
	 */
	@RequiresPermissions("system:attachements:remove")
	@Log(title = "附件", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(attachementsService.deleteAttachementsByIds(ids));
	}
	
}
